<?php
namespace Index\Controller;
use Think\Controller;
class UserController extends CommonController {
	//用户中心
    public function index(){
    	
    	
    	$id=$_SESSION['uid'];
    	$sql="select * from question where uid='$id'";
    	$question=M()->query($sql);
    	// var_dump($question);
    	$sql="select t1.*,t2.title from collect t1 inner join  question t2 on t1.qid=t2.id where t1.uid='$id'";
    	$collect=M()->query($sql);
    	$this->assign('collect',$collect);//用户中心我的收藏
    	$this->assign('question',$question);//用户中心我的贴
    	// 权限控制的一种方法// $this->is_login();
       $this->display();

    }	
    //基本设置
    public function set()
    {
    	// $this->is_login();
    	$data = M('user')->find($_SESSION['uid']);
    	// var_dump($data);
    	$this->assign('user',$data);
    	$this->display();
    }
    //我的消息
    public function msg()
    {
    	$id = $_SESSION['uid'];

    	$sql = "select t1.title,t2.create_time,t3.nickname from question t1 inner join answer t2 on t1.id=t2.qid inner join user t3 on t2.uid=t3.id where t1.uid='$id' ";
  
    	// var_dump($sql);
    	$data = M()->query($sql);
    	// var_dump($data);
    	$this->assign('data',$data);
    	$this->display();
    }


    	//我的资料修改
		public function  amendadd()
		{
			$data=I('post.');

			$id=$_SESSION['uid'];
			// var_dump(M('user')->where("id='$id'")->save($data));exit;
			if (M('user')->where("id='$id'")->save($data)) {
				 
				$_SESSION['nickname']=$data['nickname'];
				$_SESSION['city']=$data['city'];
				$_SESSION['sign']=$data['sign'];
				 Reajax(0,"修改完成");

			}else{
				Reajax(1,"修改失败");
			}
		}
		//修改密码
		public function amendpass()
		{
			$data=I('post.');

			$id=$_SESSION['uid'];
			if ($data['pass']==$data['repass']) {

				$data['password']=md5($data['pass']);

				if (M('user')->where("id='$id'")->save($data)) {
					
					Reajax(0,"修改密码成功");
				}else{
					Reajax(1,"修改密码失败");
				}
			}else{
				Reajax(1,"两次密码不一样");
			}
	
		}
		//头像上传
		public function upload()
		{
			    $upload = new \Think\Upload();// 实例化上传类
			    $upload->maxSize   =     3145728 ;// 设置附件上传大小
			    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
			    $upload->savePath  =     ''; // 设置附件上传（子）目录
			    // 上传文件 
			    $info   =   $upload->upload();
			   
			    if(!$info) {// 上传错误提示错误信息
			        $this->error($upload->getError());
			    }else{// 上传成功

					//二维数组转一维
			    	$info = current($info);
			    	//组头像路径
			        $data['face'] = "/Uploads/".$info['savepath'].$info['savename'];
			        //获取用户id
			        $data['id']=$_SESSION['uid'];
			        //把头像地址更改到session
			        $_SESSION['face']=  $data['face'];
			        //把头像地址更改到数据库
			        M('user')->save($data);
			        //返回头像
			        echo $data['face'];
			    }
		}
		//我的资料里我发帖编辑修改
		public function adduser()
		{

			$cate = M('category')->select();
	   		$tempArr=[];
	   		foreach ($cate as $row) {
	   			if ($row['fid']==0) {
	   				$tempArr[$row['id']]=$row;
	   			}
	   		}
	   		foreach ($cate as $son) {
	   			foreach ($tempArr as $key => $fater) {
	   				if ($son['fid']==$key) {
	   					$tempArr[$key]['son'][]=$son;
	   				}
	   			}
	   		}
   			$this->assign('cate',$tempArr);//分类
			$question=D('vercode')->getOne();//人类验证
	    	$this->assign('question',$question);

			$id=$_SESSION['uid'];
			// var_dump($id);
			$qid=I('get.id');
			$sql="select * from question where id='$qid' and uid=$id";
			$data=M()->query($sql);
			// var_dump($data);
			$this->assign('data',current($data));
			$this->display();
		}


		//修改我的发帖的方法
		public function addcontent()
		{
			// var_dump(I('post.'));
			$qid=I('post.qid');

			$data=M('question')->where("id='$qid'")->find();
			$adddata=I('post.');
			if (!($adddata['content']==$data['content']&&$adddata['kiss']==$data['kiss']&&$adddata['cid']==$data['cid'])) {
					$res=M('question')->where("id='$qid'")->save($adddata);
					if ($res) {

						$url=U('index/jie/index',array("id"=>$qid));

						Reajax(0,array('info'=>"修改成功",'url'=>$url));
					}else{
						Reajax(1,"修改不成功");
					}
			}
			
		}

		//PHP发QQ邮件
		public function send()
		{
			$content= <<<mjg
		//可以发以个模板在这里

mjg;
			sendmail('454326248@qq.com','主题活动通知','这个是PHP测试发邮件');
		}


}
?>